<?php 
class Account
{
protected $access_token_Request=null;
protected $_uid=null;
protected $_info=null;

public function __construct($uid=null)
{
	$this->_uid=$uid;
	$this->LoadInfo();
}

private function GenerateAT($uid)
{
	$v=$uid.time().rand();
	$ctx = hash_init('md5');
	hash_update($ctx,$v);
	$v=hash_final($ctx);
	$at=array(intval($uid),$v);
	return $at;
}

public function GetInfo()
{
	return $this->_info;  
}

public function Login($email,$pass,$bStay)
{
  	$db=GetDataBase();
  	$ctx = hash_init('md5');
	hash_update($ctx,$pass);
	$pass=hash_final($ctx);
	
	$strSqlQuery="SELECT Id,Email,Gender,UserName,UserPassport FROM account WHERE Email=:em;";
	$q = $db->fetchRow($strSqlQuery,array('em'=>$email));
	if($q==null)
	{
		return null;
	}
	elseif($q['UserPassport']!=$pass)
	{
		return null;
	}
	else
	{
		$expireTime=0;
		
		if($bStay)
		{
			$expireTime=time()+365*24*60*60;
		}		
		$at=$this->GenerateAT($q['Id']);
		setcookie("at",implode("|",$at),$expireTime,"/","fancy.com");
		
		$i=new stdclass();
		$i->UID			=intval($q['Id']);
		$i->UniqueId	=$at[1];
		$i->EMail		=$q['Email'];
		$i->UserName	=$q['UserName'];
		$i->Gender		=intval($q['Gender']);
		
		
		 $db->query("UPDATE account SET UniqueId=:atId WHERE Id=:uid",
		 		array('uid'=>$q['Id'],
		 			'atId'=>$at[1]));
		 	
		$keyName="ACCOUNT_INFO_".$at[0];
		$mc=GetMemcache();
		$accountInfo=$mc->set($keyName,$i,false,60);
		return $i;
	}
}

public function IsLogin()
{
	$at=$this->GetATFromRequest();	 
  	if($at==null)
  	{
  		return false;
  	}
  	
  	if($this->_info==null)
  	{
  		$this->_uid=$at[0];
  		if(!$this->LoadInfo())
  		{
  			return false;
  		}
  	}
  	
  	if($at[1] !=$this->_info->UniqueId)
  	{
  		return false;
  	}
  	  		
  	return true;
}

public function GetATFromRequest()
{
	if($this->access_token_Request!=null)
	{
		return $this->access_token_Request;
	}
      	
      	$access_token=null;
      	if(isset($_REQUEST['access_token']))
	{
		$access_token=$_REQUEST['access_token'];	
	}
	elseif(isset($_COOKIE['at']))
	{
		$access_token=$_COOKIE['at'];	
	}
	else
	{
		return null;
	}
	
	$temp=explode("|",$access_token,2);
	if(count(	$temp)!=2)
	{
		return null;
	}
	
	$this->access_token_Request=array(intval($temp[0]),$temp[1]);
	return $this->access_token_Request;  
}

public function Logout()
{
	  if(!$this->IsLogin())
	  {
	  	return false;
	  }
	  
	  setcookie("at","",time()-3600,"/","fancy.com");	  
	  return true;
}

public function LoadInfo()
{
	if($this->_uid!=null && $this->_info==null)
	{
		$keyName="ACCOUNT_INFO_".$this->_uid;
		
		$mc=GetMemcache();
		$this->_info=$mc->get($keyName);
		if($this->_info!=null)
		{
			return true;
		}
		else
		{
			$db=GetDataBase();
			$strSqlQuery="SELECT Id,Email,Gender,UserName,UniqueId ".
				" FROM account WHERE Id=:uid;";
			$q = $db->fetchRow($strSqlQuery,array('uid'=>$this->_uid));
			if($q!=null)
			{
				$i=new stdclass();
				$i->UID			=intval($q['Id']);
				$i->Email		=$q['Email'];
				$i->UserName	=$q['UserName'];
				$i->Gender		=intval($q['Gender']);
				$i->UniqueId	=$q['UniqueId'];
				
				$mc=GetMemcache();
				$mc->set($keyName,$i,false,60);
				$this->_info=$i;
				return true;
			}
		}
	}
	return false;
}

public function Reg($email,$pass,$userName,$gender)
{
	$db=GetDataBase();
	$strSqlQuery="SELECT Id FROM account WHERE EMail=:em;";
	$q = $db->fetchRow($strSqlQuery,array('em'=>$email));
	if($q==null)
	{
		$ctx = hash_init('md5');
		hash_update($ctx,$pass);
		$pass=hash_final($ctx);
		$strSql="INSERT INTO account(EMail,UserPassport,Gender,UserName) VALUES(:em,:p,:g,:un);";
		$db->query($strSql,
				array('em'=>$email,
					'p'=>$pass,
					'g'=>$gender,
					'un'=>$userName));
		return 0;
	}
	return -1;	  
}
}
?>